<head>
<style>
tr {
  white-space: nowrap;
}
.results {
  text-align: right;
  min-width: 6em;
  color: black;
}
</style>
<script type="text/javascript" src="jquery-1.7.js"></script>

<script>



// Tests the roundtrip time of sendRequest().
var tabid;
function testRequest() {
  $("#debuginfo").append("do ordering");	
  
 chrome.tabs.create( {url:"http://www.yihaodian.com/checkout/index.do",selected:false}, function(tab) {
		tabid = tab.id;
		
		chrome.extension.onRequest.addListener(
				 function(request, sender, sendResponse){		
				 			 	
				 	if(sender.tab.id == tabid){ 
						sendResponse({doit:true});						
					}  
				}
		);
  });   

}

function trydoExchange(id)
{
	jQuery.post("http://www.yihaodian.com/product/ajaxAddPointProduct.do",{productID:id,buyType:1}
				,function(result){				
						
					if( $(result).find("#addProductFailed").length >0 ) // failed
					{						
					}
					else
					{
						testRequest();
					}
					
    	});
}


function trylogin()
{
	$("#debuginfo").html("try login...");
	$.post("https://passport.yihaodian.com/passport/login.do",
		{ "credentials.username":"rckzc@126.com"
		,"credentials.password":"600058"},
		function(data, textStatus, jqXHR){
			//$("#debuginfo").html(data); 
		}
	);
}


function watchtheurl(cnt)
{	
	var d  = document.getElementById("productid").value;
	var numberupper = parseInt(document.getElementById("number").value);
	var c= new Date();
	var exchangeType = "isExchange";
	var exchangeTypeValue = 1;
	var s_url = "http://www.yihaodian.com/pointProduct/initPointProduct.do?productId="+d+"&"+exchangeType+"="+exchangeTypeValue+"&tt="+c; //"http://www.yihaodian.com/pointProduct/index.do?productId=1114797&isExchange=1";//"http://www.yihaodian.com/pointProduct/index.do?productId="+productid+"&isExchange=1";
	
	$("#debuginfo").html(s_url);	
	
	$("#debuginfo").append("    ");	
	$("#debuginfo").append(cnt);	
	
	$.ajax(
		{
			url: s_url,
			async: false,
			dataType: "html",
			complete : function(jqXHR, textStatus)
			{
				//$("#debugShow").html(textStatus);
			},
			error : function(jqXHR, textStatus, errorThrown){				
				$("#debuginfo").append("    ");	
				$("#debuginfo").append(textStatus);
			},
			success: function(data, textStatus, jqXHR)
			{
				//check if login not then login
				var respons = $(jqXHR.responseText);
				if(respons.find("#limitPointId").length==0)
				{
					trylogin();
				}

				//$("#debuginfo").html(jqXHR.responseText);
				var txt = respons.find("[class='red f24 fb']");//.text();	
				if(txt.length>0)
				{
				
					$("#debuginfo").append("    ");	
					$("#debuginfo").append(txt.first().text());	
				

					if( parseInt(txt.first().text()) > numberupper)
					{					
						$("#debuginfo").append("More Than " +numberupper );
						window.setTimeout( function(){ watchtheurl(cnt+1);},1000 );
					}
					else
					{	
						trydoExchange(productid);
					}	
				}
				else
				{
					$("#debuginfo").append("    ");	
					$("#debuginfo").append("error");	
					window.setTimeout( function(){ watchtheurl(cnt+1);},1000 );
				}
				
			}
		}
	);	
}


</script>
</head>
<body>
<table>
  <tr>
  	 <td>producturl<input type="text"  id="productid" value="1114797" > </td>   
      <td>less<input type="text"  id="number" value="5000">do order</td>     
    <td ><input type="button" value="start" id="btnstart" onClick="watchtheurl(0)"></td>
  </tr>
</table>

<div id="debuginfo"></div>
</body>
